﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public static class DataRowViewExtensionMethods
    {
        public static ListViewItem AsListViewItem(this DataRowView holidays, bool coloured)
        {

            #region listview columns
            // EMPLOYEE
            var result = new ListViewItem(SQLiteAccess.GetEmployee((Int64)holidays[1])[1].ToString()); // first Name

            var startDate = (DateTime)holidays[2];
            var endDate = (DateTime)holidays[3];
            // PERIOD
            result.SubItems.Add(startDate.ToShortDateString() + " - " + endDate.ToShortDateString()); // displayColumn / period (eg. 24.04 - 1.05)
            // TOTAL DAYS
            result.SubItems.Add(((endDate - startDate).Days + 1).ToString()); // GetTotal pDays

            // TYPE
            result.SubItems.Add(((Int64)holidays[4]).To<HolidayType>().GetTypeName());

            // STATUS
            var statuses = new List<String>();
            var hasBegan = startDate <= DateTime.Now;
            var isOver = endDate < DateTime.Now;
            int timeStatus;
            if (hasBegan)
            {
                timeStatus = isOver ? -1 : 0;
            }
            else
            {
                timeStatus = 1;
            }

            if (timeStatus == -1)
            {
                statuses.Add(Resources.HOLIDAY_TimeStatus_Finished);
            }
            if (timeStatus == 0)
            {
                statuses.Add(Resources.HOLIDAY_TimeStatus_InProgress);
            }
            if (timeStatus == 1)
            {
                statuses.Add(Resources.HOLIDAY_TimeStatus_Ahead);
            }
            if ((bool)holidays[5])
            {
                statuses.Add(Resources.STATUS_Pending);
            }

            result.SubItems.Add(String.Join(", ", statuses.ToArray()));

            // COMMENTS
            result.SubItems.Add(holidays[6].ToString());
            #endregion

            #region Tag which will be used to assign it to groups
            var tag = new ListViewItemTag
                          {
                              employeeID = (Int64) holidays[1],
                              month = startDate.Month,
                              timeStatus = Convert.ToSByte(timeStatus),
                              id = (Int64) holidays[0]
                          };
            result.Tag = tag;
            #endregion


            result.ForeColor = ((bool)holidays[5] && coloured) ?
                SystemColors.InactiveCaptionText :
                SystemColors.ControlText;

            return result;
        }
    }
}
